<template>
  <div class="app-container">
    <!-- 类型管理新增 -->
    <h5 class="titleInfo">
      类型管理新增
    </h5>
    <el-form ref="form"  :inline="true" :model="form" :rules="rules" label-width="130px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="类型名" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="所属分类" prop="type">
            <el-select v-model="form.type" placeholder="请选择" clearable>
              <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序">
            <el-input v-model="form.sort" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图标">
            <newUpload ref="uploadImg" :fileNum="fileNum" :width='100' :height="100"/>
            <p>请上传图片尺寸为100*100的图片大小100K以下，格式为jpg或png</p>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-switch
              v-model="form.status"
              active-text="正常"
              inactive-text="下架"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button style="margin-top:30px;margin-left:130px" type="primary" @click="submitFn('form')">立即提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import newUpload from "@/components/imgUpload/index";
import { materialTypeListAdd } from '@/api/materialType'
export default {
  name: 'MaterialTypeAdd',
  filters: {},
  components: { newUpload },
  data() {
    return {
      fileNum: 1,
      form: {
        sort: null,
        name: '',
        type: null,
        img: '',
        status: 0
      },
      rules: {
        name: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }]
      },
      typeList: [
        { id: 1, name: '手术前后对比照' },
        { id: 2, name: '美咖资料' },
        { id: 3, name: '合作意愿介绍' }
      ]
    }
  },
  mounted() {
    this.id = Number(this.$route.query.id)
    this.form.id = this.id
  },
  methods: {
    // 提交
    submitFn(e) {
      this.form.img = this.$refs.uploadImg.submitUpload()[0]
      this.$refs[e].validate(valid => {
        if (valid) {
          const datas = this.form
          materialTypeListAdd(datas).then(res => {
            if (res.data.code == 0) {
              this.$message({
                message: res.data.msg,
                type: 'success'
              })
              this.$router.push('/MaterialTypeIndex')
            } else {
              this.$message({
                message: res.data.msg,
                type: 'warning'
              })
            }
          })
        } else {
          this.$message({
            message: '必填项不能为空',
            type: 'warning'
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 0px !important;
}
.form {
  margin-left: 268px;
}
.el-switch__label--left{
  margin-right: 0
}
.el-switch__label.is-active{
  color: #409EFF!important;
  width: 100%;
  text-align: center;
  display: inline-block;
}
</style>
